import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import { getUserInfoAPI } from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '', // 保存token字符串
    myName: '',
    user_pic: '',
    uesrInfo: {} // 保存用户信息（id,username,nickname,email,user_pic）
  },
  getters: {
    username: state => state.myName, // 用户名
    nickname: state => state.uesrInfo?.nickname, // 用户昵称
    user_pic: state => state.user_pic // 用户头像
  },
  mutations: {
    // 保存token
    updateToken (state, obj) {
      state.token = obj.val
      state.myName = obj.username
      state.user_pic = obj.user_pic
    },
    // 保存用户信息
    updateUserInfo (state, val) {
      // state.uesrInfo = val
      state.user_pic = val
    }
  },
  actions: {
    // 请求用户信息
    async getUserInfoActions (store, url) {
      // const res = await getUserInfoAPI()
      console.log(getUserInfoAPI)
      store.commit('updateUserInfo', url)
    }
  },
  modules: {
  },
  plugins: [
    createPersistedState() // 注入持久化插件
  ]
})
